﻿using AutoMapper;
using AutoMapper.Internal.Mappers;
using DefenseQuestion.Application.QuestionBankApplication.Dto;
using DefenseQuestion.Application.UserApplication.Dto;
using DefenseQuestion.Repositorys;
using Microsoft.Azure.Documents;
using Newtonsoft.Json.Serialization;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DefenseQuestion.Application.QuestionBankApplication
{
    public class QuestionService : IQuestionService
    {
        private readonly IMapper mapper;

        public IBaseRepository<Question> Qusetions { get; }
        public IBaseRepository<QuestionType> QuestionTypes { get; }
        public IBaseRepository<TechType> Techtypes { get; }

        public QuestionService(IBaseRepository<Question> Qusetions, IMapper mapper,IBaseRepository<QuestionType> QuestionTypes,IBaseRepository<TechType> techtypes)
        {
            this.Qusetions = Qusetions;
            this.mapper = mapper;
            this.QuestionTypes = QuestionTypes;
            Techtypes = techtypes;
        }

        /// <summary>
        /// 题库添加
        /// </summary>
        /// <param name="Qusetion"></param>
        /// <returns></returns>
        public async Task<ResultDto<Question>> AddQusetionAsync(CreateandUpdateQuestionDto dto)
        {
            var QuestionName = Qusetions.Queryable().Where(x => x.QuestionName == dto.QuestionName).ToList();
            if (QuestionName.Count != 0)
            {
                return new ResultDto<Question> { Code = Result.Success, Message = "已有此题库" };
            }
            var list = mapper.Map<CreateandUpdateQuestionDto, Question>(dto);
            await Qusetions.AddAsync(list);
            return new ResultDto<Question> { Code = Result.Success, Message = "注册成功" };
        }

        /// <summary>
        /// 题库修改
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        public async Task<ResultDto<Question>> UpdateQusetionAsync(CreateandUpdateQuestionDto dto)
        {
            var QuestionIdList = await Qusetions.FindAsync(dto.QuestionId);

            //mapper.Map(QuestionIdList, dto);
            if (QuestionIdList.QuestionName==dto.QuestionName)
            {
                return new ResultDto<Question> { Code = Result.Success, Message = "题库名已存在" };
            }
            else
            {
                var list = mapper.Map(dto, QuestionIdList);
                await Qusetions.UpdateAsync(list);
                return new ResultDto<Question> { Code = Result.Success, Message = "修改成功" };
            }
        }

        /// <summary>
        /// 通过Id查询
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public ResultDto<Question> FindQusetion(int id)
        {
            var data = Qusetions.Queryable().FirstOrDefault(x => x.QuestionId == id);
            return new ResultDto<Question> { Code = Result.Success, Data = data };
        }

        /// <summary>
        /// 题库删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<ResultDto<bool>> DeleteQusetionAsync(int id)
        {
            var isOk = false;
            var list = Qusetions.Queryable().Where(x => x.QuestionId == id);
            foreach (var item in list.ToList())
            {
                item.DeleteName = "删除人:" + item.QuestionName;
                item.IsDel = true;
                item.DeleteDate = DateTime.Now;
                await Qusetions.UpdateAsync(item);
                isOk = true;
            }
            return new ResultDto<bool> { Code = Result.Success };
        }

        /// <summary>
        /// 题库分页查询显示
        /// </summary>
        /// <param name="QusetionName"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public QusetionListResultDto<List<Question>> GetQusetionsAsync(int TechTypeId, int pageIndex, int pageSize)
        {
            var list = Qusetions.Queryable().Where(x => x.IsDel == false).ToList();
            if (TechTypeId>0)
            {
                list = list.Where(x => x.TechTypeId== TechTypeId).ToList();
            }
            var totalCount = list.Count();
            list = list.OrderBy(x => x.QuestionId).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
            return new QusetionListResultDto<List<Question>> { Code = Result.Success, totalCount = totalCount, Data = list };
        }
        /// <summary>
        /// 题库类型递归
        /// </summary>
        /// <returns></returns>
        public ResultDto<List<QuestionTypeDto>> GetQuestionTypeDto()
        {
            var list = QuestionTypes.Queryable();
            return new ResultDto<List<QuestionTypeDto>> { Code = Result.Success, Data = GetTrees(list.ToList(),0) };
        }
        private List<QuestionTypeDto> GetTrees(List<QuestionType> types, int ParentId)
        {
            List<QuestionTypeDto> tree = new List<QuestionTypeDto>();
            var list = types.Where(x => x.ParentId == ParentId);
            foreach (var questionType in list)
            {
                var treeDto = mapper.Map<QuestionType,QuestionTypeDto>(questionType);
                treeDto.text = questionType.QuestionTypeName;
                treeDto.value = questionType.QuestionTypeId;
                treeDto.children= !GetTrees(types, questionType.QuestionTypeId).Any() ? null : GetTrees(types, questionType.QuestionTypeId);
                tree.Add(treeDto);
            }
            return tree;
        }
        /// <summary>
        /// 技术分类表
        /// </summary>
        /// <returns></returns>
        public ResultDto<List<TechTypeDto>> GetTechType()
        {
            List<TechTypeDto> thchens= new List<TechTypeDto>();
            var list=Techtypes.Queryable().ToList();
            foreach (var item in list)
            {
               var thech=new TechTypeDto();   
                thech.text = item.TypeName;
                thech.value = item.TechTypeId;
                thchens.Add(thech);
                
            }
            return new ResultDto<List<TechTypeDto>> { Code = Result.Success, Data = thchens };
        }
    }
}
